<template>
  <div class="auth-wrapper">
    <!-- 背景装饰元素 -->
    <div class="bg-decoration">
      <!-- 咖啡豆装饰 -->
      <div class="coffee-bean bean-1"></div>
      <div class="coffee-bean bean-2"></div>
      <div class="coffee-bean bean-3"></div>
      <div class="coffee-bean bean-4"></div>
      <div class="coffee-bean bean-5"></div>

      <!-- 咖啡滴装饰 -->
      <div class="coffee-drop drop-1"></div>
      <div class="coffee-drop drop-2"></div>
      <div class="coffee-drop drop-3"></div>

      <!-- 咖啡蒸汽效果 -->
      <div class="steam steam-1"></div>
      <div class="steam steam-2"></div>
    </div>

    <el-card class="auth-card">
      <template #header>
        <div class="card-header">
          <div class="coffee-cup-icon">
            <i class="el-icon-plus" />
          </div>
          <h1>用户注册</h1>
          <p class="tagline">开启咖啡之旅</p>
        </div>
      </template>

      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="70px"
        size="medium"
        @submit.native.prevent="register"
        class="login-form"
      >
        <!-- 用户名输入 -->
        <el-form-item label="用户名" prop="username" class="form-item">
          <div class="input-group">
            <div class="input-icon">
              <i class="el-icon-user"></i>
            </div>
            <el-input
              v-model="form.username"
              placeholder="3-50位字母/数字/下划线"
              clearable
              class="custom-input"
            />
          </div>
        </el-form-item>

        <!-- 邮箱输入 -->
        <el-form-item label="邮箱" prop="email" class="form-item">
          <div class="input-group">
            <div class="input-icon">
              <i class="el-icon-envelope"></i>
            </div>
            <el-input
              v-model="form.email"
              type="email"
              placeholder="example@mail.com"
              clearable
              class="custom-input"
            />
          </div>
        </el-form-item>

        <!-- 手机号输入 -->
        <el-form-item label="手机号" prop="phone" class="form-item">
          <div class="input-group">
            <div class="input-icon">
              <i class="el-icon-phone"></i>
            </div>
            <el-input
              v-model="form.phone"
              maxlength="11"
              placeholder="11位手机号"
              clearable
              class="custom-input"
            />
          </div>
        </el-form-item>

        <!-- 密码输入 -->
        <el-form-item label="密码" prop="password" class="form-item">
          <div class="input-group">
            <div class="input-icon">
              <i class="el-icon-lock"></i>
            </div>
            <el-input
              v-model="form.password"
              type="password"
              show-password
              placeholder="8位以上，含大小写、数字、特殊字符"
              clearable
              class="custom-input"
            />
          </div>
        </el-form-item>

        <!-- 密码强度提示 -->
        <el-form-item>
          <password-strength :password="form.password" />
        </el-form-item>

        <!-- 注册按钮 -->
        <el-form-item class="form-item">
          <el-button
            type="primary"
            native-type="submit"
            :loading="loading"
            :disabled="!canSubmit"
            style="width: 100%"
            class="login-btn"
          >
            <span>注册账号</span>
            <i class="el-icon-arrow-right ml-2"></i>
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer-tip">
        已有账号？
        <router-link to="/login" class="register-link">
          <span>立即登录</span>
          <i class="el-icon-arrow-right ml-1"></i>
        </router-link>
      </div>

      <!-- 装饰元素 -->
      <div class="card-decoration top-left"></div>
      <div class="card-decoration bottom-right"></div>
    </el-card>
  </div>
</template>

<script>
import api from '@/api/request'

// 密码强度组件（保持原有逻辑）
const PasswordStrength = {
  props: { password: String },
  computed: {
    level() {
      const p = this.password || ''
      let score = 0
      if (p.length >= 8) score++
      if (/[A-Z]/.test(p)) score++
      if (/[a-z]/.test(p)) score++
      if (/\d/.test(p)) score++
      if (/[!@#$%^&*(),.?":{}|<>]/.test(p)) score++
      return score
    },
    hint() {
      if (!this.password) return ''
      if (this.level >= 5) return '强'
      if (this.level >= 3) return '中'
      return '弱'
    },
    color() {
      if (this.level >= 5) return 'green'
      if (this.level >= 3) return 'orange'
      return 'red'
    }
  },
  template: `
    <div :style="{color, fontSize: '12px'}">
      密码强度：{{ hint }}
    </div>
  `
}

export default {
  name: 'Register',
  components: { PasswordStrength },
  data() {
    return {
      form: {
        username: '',
        email: '',
        phone: '',
        password: ''
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_]{3,50}$/, message: '3-50位字母/数字/下划线', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的11位手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>]).{8,}$/, message: '8位以上，含大小写、数字、特殊字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    canSubmit() {
      // 保持原有验证逻辑
      const { username, email, phone, password } = this.form
      return (
        username &&
        email &&
        /^1[3-9]\d{9}$/.test(phone) &&
        /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>]).{8,}$/.test(password)
      )
    }
  },
  methods: {
    // 保持原有注册方法逻辑
    async register() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        this.loading = true
        try {
          await api.post('/register', this.form)
          this.$message.success('注册成功，请登录')
          this.$router.replace('/login')
        } catch (e) {
          this.$message.error(e?.response?.data?.detail || '注册失败')
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style scoped>
/* 基础样式与背景（复用登录页） */
.auth-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  padding: 20px;
  /* 咖啡主题渐变背景 */
  background: linear-gradient(135deg, #3E2723 0%, #5D4037 30%, #8D6E63 60%, #D7CCC8 100%);
}

/* 背景装饰容器（复用登录页） */
.bg-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* 咖啡豆装饰（复用登录页） */
.coffee-bean {
  position: absolute;
  width: 60px;
  height: 30px;
  background: linear-gradient(90deg, #5D4037 0%, #8D6E63 100%);
  border-radius: 50% / 100% 100% 0 0;
  opacity: 0.6;
  transform: rotate(45deg);
  animation: float 20s infinite ease-in-out;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
}

.coffee-bean::after {
  content: '';
  position: absolute;
  width: 60px;
  height: 30px;
  background: linear-gradient(90deg, #8D6E63 0%, #5D4037 100%);
  border-radius: 50% / 0 0 100% 100%;
  top: 15px;
  left: 0;
}

/* 不同咖啡豆的位置和动画延迟（复用登录页） */
.bean-1 { top: 10%; left: 15%; animation-delay: 0s; }
.bean-2 { top: 70%; right: 20%; animation-delay: 2s; width: 70px; height: 35px; }
.bean-3 { bottom: 15%; left: 25%; animation-delay: 4s; }
.bean-4 { top: 40%; right: 10%; animation-delay: 6s; width: 50px; height: 25px; }
.bean-5 { top: 25%; left: 70%; animation-delay: 8s; }

/* 咖啡滴装饰（复用登录页） */
.coffee-drop {
  position: absolute;
  width: 20px;
  height: 30px;
  background-color: #5D4037;
  border-radius: 0 0 50% 50%;
  opacity: 0.4;
  animation: drip 8s infinite ease-in-out;
}

.drop-1 { top: 20%; right: 30%; animation-delay: 0s; }
.drop-2 { top: 50%; left: 10%; animation-delay: 2s; width: 15px; height: 25px; }
.drop-3 { bottom: 30%; right: 40%; animation-delay: 4s; width: 25px; height: 35px; }

/* 蒸汽效果（复用登录页） */
.steam {
  position: absolute;
  width: 80px;
  height: 80px;
  background: radial-gradient(circle, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0) 70%);
  border-radius: 50%;
  animation: rise 15s infinite ease-in-out;
}

.steam-1 { top: 15%; left: 60%; }
.steam-2 { bottom: 25%; right: 25%; width: 100px; height: 100px; animation-delay: 5s; }

/* 注册卡片（复用登录页） */
.auth-card {
  width: 420px;
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  background-color: #FFF8E7;
  background-image:
    radial-gradient(#D7CCC8 2px, transparent 2px),
    radial-gradient(#D7CCC8 2px, #FFF8E7 2px);
  background-size: 40px 40px;
  background-position: 0 0, 20px 20px;
  border: none;
  z-index: 10;
  overflow: hidden;
  position: relative;
  transform: perspective(1000px) rotateY(0deg);
  transition: transform 0.5s ease;
}

.auth-card:hover {
  transform: perspective(1000px) rotateY(2deg);
}

/* 卡片装饰元素（复用登录页） */
.card-decoration {
  position: absolute;
  width: 120px;
  height: 120px;
  opacity: 0.1;
  z-index: 0;
}

.top-left {
  top: -60px;
  left: -60px;
  background: radial-gradient(circle, #8B5A2B 0%, transparent 70%);
}

.bottom-right {
  bottom: -60px;
  right: -60px;
  background: radial-gradient(circle, #8B5A2B 0%, transparent 70%);
}

/* 卡片头部（调整为注册主题） */
.card-header {
  text-align: center;
  padding: 30px 20px 20px;
  position: relative;
}

.coffee-cup-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 15px;
  background: linear-gradient(135deg, #8B5A2B 0%, #D2B48C 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 5px 15px rgba(139, 90, 43, 0.3);
  position: relative;
  z-index: 1;
}

.coffee-cup-icon::before {
  content: '';
  position: absolute;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  background: linear-gradient(135deg, #D2B48C 0%, #8B5A2B 100%);
  z-index: -1;
  animation: pulse 3s infinite ease-in-out;
}

.coffee-cup-icon .el-icon-plus {
  font-size: 40px;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.card-header h1 {
  font-size: 28px;
  font-weight: 700;
  color: #5D4037;
  margin: 0 0 8px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.tagline {
  font-size: 14px;
  color: #8D6E63;
  margin: 0;
  font-style: italic;
}

/* 表单样式（复用登录页） */
.login-form {
  padding: 10px 30px 20px;
  position: relative;
  z-index: 1;
}

.form-item {
  margin-bottom: 24px;
}

/* 输入框组（复用登录页） */
.input-group {
  display: flex;
  align-items: center;
}

.input-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #8B5A2B 0%, #6D4C41 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px 0 0 12px;
  font-size: 18px;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
}

/* 自定义输入框（复用登录页） */
.custom-input .el-input__inner {
  height: 48px;
  border-radius: 0 12px 12px 0;
  border: 2px solid #D2B48C;
  border-left: none;
  background-color: rgba(255, 255, 255, 0.9);
  color: #5D4037;
  font-size: 15px;
  padding: 0 15px;
  transition: all 0.3s ease;
}

.custom-input .el-input__inner::placeholder {
  color: #A1887F;
  font-style: italic;
}

.custom-input .el-input__inner:focus {
  border-color: #8B5A2B;
  box-shadow: 0 0 0 3px rgba(139, 90, 43, 0.1);
  background-color: #fff;
}

/* 注册按钮（复用登录页按钮样式） */
.login-btn {
  background: linear-gradient(135deg, #8B5A2B 0%, #5D4037 100%) !important;
  border: none !important;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 12px !important;
  color: white !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 5px 15px rgba(139, 90, 43, 0.3);
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: 0.5s;
}

.login-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(139, 90, 43, 0.4);
}

.login-btn:hover::before {
  left: 100%;
}

.login-btn:active {
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(139, 90, 43, 0.3);
}

/* 底部提示（调整为登录链接） */
.footer-tip {
  text-align: center;
  margin: 10px 30px 35px;
  font-size: 14px;
  color: #6D4C41;
  position: relative;
  z-index: 1;
}

/* 登录链接（复用登录页注册链接样式） */
.register-link {
  color: #8B5A2B;
  text-decoration: none;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  transition: all 0.3s ease;
  position: relative;
}

.register-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #8B5A2B;
  transition: width 0.3s ease;
}

.register-link:hover {
  color: #5D4037;
  transform: translateX(3px);
}

.register-link:hover::after {
  width: 100%;
}

/* 动画定义（复用登录页） */
@keyframes float {
  0%, 100% { transform: rotate(45deg) translateY(0px) scale(1); }
  50% { transform: rotate(45deg) translateY(-20px) scale(1.1); }
}

@keyframes drip {
  0%, 100% { transform: translateY(0px) scale(1); opacity: 0.4; }
  50% { transform: translateY(20px) scale(1.2); opacity: 0.6; }
}

@keyframes rise {
  0% { transform: translateY(0px) scale(1); opacity: 0.3; }
  50% { transform: translateY(-30px) scale(1.3); opacity: 0.1; }
  100% { transform: translateY(-60px) scale(1.5); opacity: 0; }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 0.8; }
  50% { transform: scale(1.1); opacity: 1; }
}

/* 响应式调整（复用登录页） */
@media (max-width: 480px) {
  .auth-card {
    width: 100%;
    max-width: 350px;
    border-radius: 20px;
  }

  .card-header {
    padding: 25px 15px 15px;
  }

  .coffee-cup-icon {
    width: 70px;
    height: 70px;
  }

  .coffee-cup-icon .el-icon-plus {
    font-size: 34px;
  }

  .card-header h1 {
    font-size: 24px;
  }

  .login-form {
    padding: 5px 20px 15px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .input-icon {
    width: 44px;
    height: 44px;
  }

  .custom-input .el-input__inner {
    height: 44px;
    font-size: 14px;
  }

  .login-btn {
    height: 46px;
    font-size: 15px;
  }

  .footer-tip {
    margin: 5px 20px 30px;
    font-size: 13px;
  }
}
</style>